package com.arges.web.page;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

public class WebUtils
{
	public static <pojo, bean> PageInfo<bean> pojoToBeanPage(PageInfo<pojo> pageInfo, Class<bean> clazz)
	{
		PageInfo<bean> beanPage = new PageInfo<bean>();
		beanPage.setPageSize(pageInfo.getPageSize());
		beanPage.setPageNo(pageInfo.getPageNo());
		beanPage.setHasNext(pageInfo.isHasNext());
		beanPage.setHasPre(pageInfo.isHasPre());
		beanPage.setTotalCount(pageInfo.getTotalCount());
		beanPage.setTotalPage(pageInfo.getTotalPage());
		beanPage.setOrder(pageInfo.getOrder());
		if (null != pageInfo.getOrderByName())
		{
			beanPage.setOrderByName(pageInfo.getOrderByName());
		}
		if (null != pageInfo.getResult() && !pageInfo.getResult().isEmpty())
		{
			List<pojo> pojoResult = pageInfo.getResult();
			List<bean> beanrResult = new ArrayList<bean>();
			try
			{
				for (pojo p : pojoResult)
				{
					bean b = clazz.newInstance();
					BeanUtils.copyProperties(p, b);
					beanrResult.add(b);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			beanPage.setResult(beanrResult);

		}
		return beanPage;
	}

	public static <pojo, bean> PageInfo<pojo> beanToPojoPage(PageInfo<bean> pageInfo, Class<pojo> clazz)
	{
		PageInfo<pojo> beanPage = new PageInfo<pojo>();
		beanPage.setPageSize(pageInfo.getPageSize());
		beanPage.setPageNo(pageInfo.getPageNo());
		beanPage.setHasNext(pageInfo.isHasNext());
		beanPage.setHasPre(pageInfo.isHasPre());
		beanPage.setTotalCount(pageInfo.getTotalCount());
		beanPage.setTotalPage(pageInfo.getTotalPage());
		beanPage.setOrder(pageInfo.getOrder());
		if (null != pageInfo.getOrderByName())
		{
			beanPage.setOrderByName(pageInfo.getOrderByName());
		}
		if (null != pageInfo.getResult() && !pageInfo.getResult().isEmpty())
		{
			List<bean> beanResult = pageInfo.getResult();
			List<pojo> pojorResult = new ArrayList<pojo>();
			try
			{
				for (bean p : beanResult)
				{
					pojo b = clazz.newInstance();
					BeanUtils.copyProperties(p, b);
					pojorResult.add(b);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			beanPage.setResult(pojorResult);

		}
		return beanPage;
	}

}
